package util.tools.stoich;

import util.core.Format;
import util.core.Molecule;

public class StoiData {
    public double getVal() {
        return val;
    }

    public char getType() {
        return type;
    }

    public Molecule getMolecule() {
        return molecule;
    }

    double val;//grams litres or mol
    char type;//g-mass l-volume m-mol
    Molecule molecule;

    final String[] prefixes = {"giga",
            "mega",
            "kilo",
            "hecto",
            "deca",
            "BASE",
            "deci",
            "centi",
            "milli",
            "micro",
            "nano",
            "pico"
    };
    final double[] prefixVals = {1000000000,
            1000000,
            1000,
            100,
            10,
            1,
            0.1,
            0.01,
            0.001,
            0.000001,
            0.000000001,
            0.000000000001
    };

    public StoiData(Molecule molecule, double val, String prefix, String type) {
        for (int i = 0; i < prefixes.length; i++) {
            if (prefixes[i].equals(prefix))
                this.val = val * prefixVals[i];
        }
        if (type.equals("g"))
            this.type = 'g';
        else if (type.charAt(0) == 'L')
            this.type = 'l';
        else if (type.equals("mol"))
            this.type = 'm';
        else
            this.type = '?';
        this.molecule = molecule;
    }


    public String toString() {
        String unit = "";
        switch (type) {
            case 'g': {
                unit = "gram";
                break;
            }
            case 'l': {
                unit = "liter";
                break;
            }
            case 'm': {
                unit = "mol";
                break;
            }
        }

        if (val != 1)
            unit += "s";

        return val + " " + unit + " of " + Format.toFormattedString(molecule);
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof StoiData))
            return false;
        StoiData s = (StoiData) o;
        return s.getMolecule().equals(molecule) && s.getVal() == val && s.getType() == type;
    }
}
